package com.woniuxy.controller;

import com.woniuxy.pojo.ChatMessage;
import com.woniuxy.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @ClassName WsController webScoket
 * @Description T0D0
 * @Author ykq11
 * @Date 2022/2/25 15:21
 * @Version 1.0
 **/
@RestController
public class WsController {
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @MessageMapping("/ws/chat")
    public void  handleMessage(Authentication authentication, ChatMessage chatMessage){
        Admin admin = (Admin)authentication.getPrincipal();
        chatMessage.setFrom(admin.getUsername());
        chatMessage.setFormNickname(admin.getName());
        chatMessage.setDataTime(LocalDateTime.now());
        simpMessagingTemplate.convertAndSendToUser(chatMessage.getTo(),"/queue/chat",chatMessage);
    }
}
